package com.g.localmsg.starter.task;

import com.g.localmsg.starter.config.DistributedTransactionProperties;
import com.g.localmsg.starter.service.TransactionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

public class TxRetryTask {


    private static final Logger log = LoggerFactory.getLogger(TxRetryTask.class);

    private TransactionService transactionService;
    private DistributedTransactionProperties properties;

    public TxRetryTask(TransactionService transactionService, DistributedTransactionProperties properties) {
        this.transactionService = transactionService;
        this.properties = properties;
    }


    @Scheduled(fixedDelayString = "${ldtx.retry.scan-interval:30000}")
    public void retryFailedTransactions() {
        if (!properties.isEnabled()) {
            return;
        }
        log.debug("开始扫描需要重试的事务...");
        try {
            transactionService.retryFailedTransaction(properties.getRetry().getBatchSize());
        } catch (Exception e) {
            log.error("事务重试任务执行出错", e);
        }
    }
}
